package com.kadron.itemtype;

import java.io.Serializable;

import org.jboss.seam.Component;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;

import com.kadron.common.KConst;
import com.kadron.creator.KItemTtypeModelCreator;
import com.kadron.dao.KImageDAO;
import com.kadron.dao.KItemTypeDAO;
import com.kadron.datamodel.KDataModel;
import com.kadron.datamodel.KItemTypeModelDataProvider;
import com.kadron.entity.ItemType;
import com.kadron.manager.KMessageManager;
import com.kadron.manager.KSelection;

@AutoCreate
@Scope(ScopeType.PAGE)
@Name("kItemTypeListManager")
public class KItemTypeListManager extends KMessageManager implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1993382100801499397L;
	
	private KDataModel itemTypeList;
	private KItemTypeModelDataProvider dataProvider;
	
	@In
	private KItemTypeDAO itemTypeDAO;
	@In
	private KImageDAO imageDAO;
	
	@Create
	public void init() {
		dataProvider = new KItemTypeModelDataProvider(new KItemTtypeModelCreator(itemTypeDAO, imageDAO), itemTypeDAO);
		itemTypeList = new KDataModel(dataProvider);
	}

	public KDataModel getItemTypeList() {
		return itemTypeList;
	}
	
	public String edit(Long id) {
		if (id == null) {
			bad(KConst.BAD_THING);
			return null;
		}
		KSelection kselection = (KSelection) Component.getInstance("kSelection");
		kselection.setItemId(id);
		return "e_item_type";
	}
	
	public String stock(Long itemTypeId) {
		if (itemTypeId == null) {
			bad(KConst.BAD_THING);
			return null;
		}
		KSelection kselection = (KSelection) Component.getInstance("kSelection");
		kselection.setItemId(itemTypeId);
		return "stock_item";
	}
	
	public void delete(ItemType it) {
		if (it == null) {
			bad(KConst.BAD_THING);
			return;
		}
		itemTypeList.remove(it);
		itemTypeDAO.delete(it);
	}
}
